package com.gouda.core.service.impl.useCity;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.gouda.core.dao.UseCityDao;
import com.gouda.core.dto.useCity.UseCityDto;
import com.gouda.core.entity.UseCityEntity;
import com.gouda.core.enums.GeneralStateEnum;
import com.gouda.core.service.uesCity.UseCityService;
import com.gouda.core.service.uesCity.UseDistrictService;
import org.amethyst.atlas.basic.manufactory.specifics.DBEntityFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by souu on 15/12/5.
 */
@Service
public class UseCityServiceImpl implements UseCityService {

    @Autowired
    private UseCityDao useCityDao;

    @Autowired
    private UseDistrictService useDistrictService;

    @Override
    public List<UseCityDto> getAllUseCity() {
        List<UseCityEntity> list = this.useCityDao.getAllUseCity();
        if (list == null || list.isEmpty()) {
            return null;
        }
        List<UseCityDto> result = Lists.newArrayList();
        for (UseCityEntity entity : list) {
            result.add(this.copyDto(entity));
        }
        return result;
    }

    private UseCityDto copyDto(UseCityEntity entity) {
        UseCityDto dto = new UseCityDto();
        dto.setCityName(entity.getCityName());
        dto.setId(entity.getId());
        dto.setIsall(entity.getIsall());
        if (GeneralStateEnum.fromInteger(dto.getIsall()) == GeneralStateEnum.NO) {
            dto.setUseDistricts(this.useDistrictService.getUseDistrictByCity(entity.getId()));
        }
        return dto;
    }

    @Override
    public boolean isUseCity(String cityName) {
        if (Strings.isNullOrEmpty(cityName)) {
            return false;
        }

        UseCityEntity useCityEntity = this.useCityDao.getUseCityByCityName(cityName);
        if (useCityEntity != null) {
            return true;
        }
        return false;
    }

    @Override
    public void createUseCity(String cityName, GeneralStateEnum isallEnum) {
        if (Strings.isNullOrEmpty(cityName) || isallEnum == null) {
            return;
        }
        UseCityEntity useCity = DBEntityFactory.getFactory().generateDBEntity(UseCityEntity.class);
        useCity.setCityName(cityName);
        useCity.setCreateTime(new Date());
        useCity.setIsall(isallEnum.getCode());
        useCity.setState(GeneralStateEnum.YES.getCode());
        this.useCityDao.insert(useCity);
    }

    @Override
    public void updateUseCity(long id, String cityName, GeneralStateEnum isallEnum, GeneralStateEnum stateEnum) {
        if (id <= 0) {
            return;
        }
        UseCityEntity useCity = this.useCityDao.unique(UseCityEntity.class, id);
        boolean isUpdate = false;
        if (useCity == null) {
            return;
        }
        if (!Strings.isNullOrEmpty(cityName)) {
            useCity.setCityName(cityName);
            isUpdate = true;
        }
        if (isallEnum != null) {
            useCity.setIsall(isallEnum.getCode());
            isUpdate = true;
        }
        if (null != stateEnum) {
            useCity.setState(stateEnum.getCode());
            isUpdate = true;
        }
        if (isUpdate) {
            this.useCityDao.update(useCity);
        }
    }

}
